﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TesteSorteio
{
         public class Player
        {
            Texture2D t_PlayerUp;
            Texture2D t_PlayerDown;

            Texture2D t_PlayerLeft;
            Texture2D t_PlayerRight;
            int intDirection;

            Vector2 Position;

            int Velocity = 4;

            KeyboardState kState;
            KeyboardState lastKState;

            enum DirectionPlayer { Up, Down, Left, Right };

            DirectionPlayer Direction = DirectionPlayer.Up;

            Game1 Game;
            TiroPersonagem TiroPersonagem;

            Vector2 OriginShoot;

            public Player(Game1 Game)
            {
                this.Position = new Vector2(300, 300);
                this.Game = Game;

                TiroPersonagem = new TiroPersonagem(Game.Content);

                LoadImages();
            }

            private void LoadImages()
            {
                t_PlayerUp = Game.Content.Load<Texture2D>("PlayerUp");

                t_PlayerDown = Game.Content.Load<Texture2D>("PlayerDown");

                t_PlayerLeft = Game.Content.Load<Texture2D>("PlayerLeft");

                t_PlayerRight = Game.Content.Load<Texture2D>("PlayerRight");

                
                OriginShoot = new Vector2(t_PlayerUp.Width / 2,
                    t_PlayerUp.Height / 2);
            }

            public void Draw(SpriteBatch spriteBatch)
            {
                spriteBatch.Begin();

                TiroPersonagem.Draw(spriteBatch);

                if (Direction == DirectionPlayer.Up)
                {
                    spriteBatch.Draw(t_PlayerUp, Position, Color.White);
                    intDirection = 0;
                }
                else if (Direction == DirectionPlayer.Down)
                {
                    spriteBatch.Draw(t_PlayerDown, Position, Color.White);
                    intDirection = 1;
                }
                else if (Direction == DirectionPlayer.Left)
                {
                    spriteBatch.Draw(t_PlayerLeft, Position, Color.White);
                    intDirection = 2;
                }
                else if (Direction == DirectionPlayer.Right)
                {
                    spriteBatch.Draw(t_PlayerRight, Position, Color.White);
                    intDirection = 3;
                }

                spriteBatch.End();
            }

            public void Update(GameTime gameTime)
            {
                kState = Keyboard.GetState();
                TiroPersonagem.Update(gameTime);

                if (kState.IsKeyDown(Keys.Up))
                {
                    Direction = DirectionPlayer.Up;
                    Position.Y -= Velocity;
                }
                else if (kState.IsKeyDown(Keys.Down))
                {
                    Direction = DirectionPlayer.Down;
                    Position.Y += Velocity;
                }
                else if (kState.IsKeyDown(Keys.Left))
                {
                    Direction = DirectionPlayer.Left;
                    Position.X -= Velocity;
                }
                else if (kState.IsKeyDown(Keys.Right))
                {
                    Direction = DirectionPlayer.Right;
                    Position.X += Velocity;
                }
                if (kState.IsKeyDown(Keys.Space))
                {
                     TiroPersonagem.DisparoTiro(Position,intDirection);
                }
            }

        }
    }
